package tp.pr5.items;

import tp.pr5.NavigationModule;
import tp.pr5.RobotEngine;

/**
 * An item that represents fuel. This item can be used at least once and it provides power energy to the robot. When the item is used the configured number of times, then it must be removed from the robot inventory
 * @author usuario_local
 *
 */
public class Fuel extends Item {
	int power;
	int times;
	
	/**
	 * Constructor
	 * @param id
	 * @param description
	 * @param power
	 * @param times
	 */
	public Fuel(String id, String description, int power, int times) {// constructora
		super(id, description);
		this.power = power;
		this.times = times;
	}
	/**
	 * Fuel can be used as many times as it was configured
	 */
	@Override
	public boolean canBeUsed() {
		return (this.times > 0);
	}
	/**
	 * Using the fuel provides energy to the robot (if it can be used)
	 */
	/*@Override
	public boolean use(RobotEngine r, Place p) {
		if(this.canBeUsed()){			
			r.addFuel(this.power);
			//p.getInventory().pickItem(this.getId());
			this.times--;
			System.out.println(r.getStatus());
			if (this.times <= 0){
				r.getInventory().pickItem(this.getId());	
				System.out.println("WALL·E says: What a pity! I have no more "+this.getId()+" in my inventory");
			}
			if(r.getFuel() <= 0){
				r.setFuel(0);
			}
			return true;
		}else{
			return false;
		}
		
	}*/
	
	/**
	 * To String override
	 */
	public String toString(){
		return this.getId()+": "+super.toString()+"// power="+this.power+", times="+this.times;
		
	}
	/**
	 * Adds the fuel to the fuel pool
	 */
	@Override
	public boolean use(RobotEngine r, NavigationModule nav) {
		if(this.canBeUsed()){			
			r.addFuel(this.power);
			//p.getInventory().pickItem(this.getId());
			this.times--;
			//System.out.println(r.getStatus());			
			if(r.getFuel() <= 0){
				r.setFuel(0);
			}
			return true;
		}else{
			return false;
		}
	}
	/**
	 * detracts the fuel from the fuel pool
	 */
	@Override
	public boolean unUse(RobotEngine r, NavigationModule nav) {
		r.addFuel(this.power*(-1));		
		this.times++;					
		if(r.getFuel() <= 0){
			r.setFuel(0);
		}
		return true;
	}

}
